package com.homer.android.sdk.command.uart;

import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import com.homer.android.sdk.devcie.status.IEspStatusUart;
import com.homer.android.sdk.type.devcie.status.EspStatusUart;
import com.homer.android.sdk.type.net.HeaderPair;
import com.homer.android.sdk.type.user.builder.BEspUser;
import com.homer.iot.esptouch.base.api.EspBaseApiUtil;

public class EspCommandUartPostStatusInternet implements IEspCommandUartPostStatusInternet
{
    private final static Logger log = Logger.getLogger(EspCommandUartPostStatusInternet.class);
    
    private boolean postUartStatus(String deviceNumber,String base64Param)
    {
        
        JSONObject jsonObject = new JSONObject();        
        String headerKey = Authorization;
        String headerValue = Token + " " + deviceNumber;
        HeaderPair header = new HeaderPair(headerKey, headerValue);
        
        JSONObject result = null;
        try
        {
            
            jsonObject.put("deviceNumber", deviceNumber);
            jsonObject.put("userToken", BEspUser.getBuilder().getInstance().getUserKey());
            jsonObject.put("param", base64Param);
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        
        String url = URL;
        result = EspBaseApiUtil.Post(url, jsonObject, header);
        int status = -1;
        try
        {
            if (result != null)
                status = Integer.parseInt(result.getString(Status));
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        if (status == HttpStatus.SC_OK)
        {
        	
            return true;
        }
        else
        {
        	
            return false;
        }
        
    }
    
    @Override
    public boolean doCommandUartPostStatusInternet(String deviceNumber, String base64Param)
    {
    	boolean result = postUartStatus(deviceNumber, base64Param);
        log.debug(Thread.currentThread().toString() + "##doCommandUartPostStatusInternet(deviceNumber=[" + deviceNumber
            + "],base64Param=[" + base64Param + "]): " + result);
        return result;
    }
    
}
